﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hutech.CRM.Library.Views;
using nvn.Core.MVP;
using Hutech.CRM.Library.Models.Entities;
using Hutech.CRM.Library.Models.BusinessAccessLayer.BusinessObjects;
using Hutech.CRM.Library.Models.BusinessAccessLayer;
using nvn.Library.Patterns;

namespace Hutech.CRM.Library.Presenters
{
    public class ListDanhMucPresenter : BasePresenter<IListDanhMucView>
    {
        public ListDanhMucPresenter(IListDanhMucView view)
            : base(view)
        {
            view.IsActive_Checked += new EventHandler<EventArgs>(view_IsActive_Checked);
            view.ListLoaiDanhMuc_ItemCommand += new EventHandler<ItemCommandArgs<DanhMucThongTin>>(view_ListLoaiDanhMuc_ItemCommand);
            view.Reload_EvetAgrs += new EventHandler<OneParameterEventAgrs<DanhMucThongTin>>(view_Reload_EvetAgrs);
        }

        void view_Reload_EvetAgrs(object sender, OneParameterEventAgrs<DanhMucThongTin> e)
        {
            DanhMucThongTin _obj = e.myType;
            List<DanhMucThongTin> listDanhMuc = new List<DanhMucThongTin>();
            listDanhMuc = GetDataList(view.ActiveItem, _obj.DanhMucGuid);
            int countList = 0;
            countList = listDanhMuc.Count;
            //if (countList != 0)
            //{
            view.AllItemCategories = listDanhMuc;
            view.CountLine = countList.ToString();
            //}
        }

        void view_ListLoaiDanhMuc_ItemCommand(object sender, ItemCommandArgs<DanhMucThongTin> e)
        {
            Guid DataKeyValue = new Guid(e.parameterValue);
            IDanhMucThongTinBAL item = new DanhMucThongTinBAL();
            DanhMucThongTin danhMuc = e.myType;
            switch (e.parameterName)
            {
                case "EditCommand":
                    view.EditItemIndex = e.itemIndex.Value;
                    view.AllItemCategories = GetDataList(view.ActiveItem, danhMuc.LoaiDanhMucGuid);
                    break;
                case "ApplyCommand":
                    if (!item.CheckExitDanhMuc(danhMuc.DanhMucName, danhMuc.DanhMucGuid))
                    {
                        if (item.Save(danhMuc))
                        {
                            view.EditItemIndex = -1;
                            view.AllItemCategories = GetDataList(view.ActiveItem, danhMuc.LoaiDanhMucGuid);
                            return;
                        }
                        view.ErrorMessage = "Thực hiện thất bại";
                        return;
                    }
                    view.ErrorMessage = "Tên danh mục đã tồn tại";
                    break;

                case "DeleteCommand":
                    if (item.DeleteKind(DataKeyValue))
                    {
                        view.AllItemCategories = GetDataList(view.ActiveItem, danhMuc.LoaiDanhMucGuid);
                        return;
                    }
                    view.ErrorMessage = "Thực hiện thất bại";
                    break;

                case "CancelCommand":
                    view.EditItemIndex = -1;
                    view.AllItemCategories = GetDataList(view.ActiveItem, danhMuc.LoaiDanhMucGuid);
                    break;
            }
        }

        void view_IsActive_Checked(object sender, EventArgs e)
        {
            LoadDanhSachLoaiDanhMuc(view.ActiveItem, view.LoaiDanhMucGuid);
        }

        private List<DanhMucThongTin> GetDataList(bool isActive, Guid loaiDanhMucGuid)
        {
            IDanhMucThongTinBAL item = new DanhMucThongTinBAL();
            List<DanhMucThongTin> listDanhMuc = new List<DanhMucThongTin>();
            listDanhMuc = item.GetAllKindByIsActive(isActive, loaiDanhMucGuid);
            int countList = 0;
            countList = listDanhMuc.Count;
            view.CountLine = countList.ToString();
            return listDanhMuc;
        }
        private void LoadDanhSachLoaiDanhMuc(bool isActive, Guid loaiDanhMucGuid)
        {
            List<DanhMucThongTin> listDanhMuc = new List<DanhMucThongTin>();
            listDanhMuc = GetDataList(view.ActiveItem, loaiDanhMucGuid);
            int countList = 0;
            countList = listDanhMuc.Count;
            view.EditItemIndex = -1;
            //if (countList != 0)
            //{
            view.AllItemCategories = listDanhMuc;
            view.CountLine = countList.ToString();
            //}
        }

    }
}
